﻿using System.Collections;

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

namespace Assets.XyzCode.Fight.Game.Enemy
{
    /// <summary>
    /// 敌人工厂类
    /// </summary>
    public sealed class EnemyFactory
    {
        /// <summary>
        /// 私有化类默认构造器
        /// </summary>
        private EnemyFactory()
        {
        }

        /// <summary>
        /// ( 异步方式 ) 根据敌人 Id 创建敌人对象
        /// </summary>
        /// <param name="enemyId">敌人 Id</param>
        /// <returns>期货</returns>
        static public Future CreateByIdAsync(int enemyId)
        {
            return new Future(enemyId);
        }

        /// <summary>
        /// 期货
        /// </summary>
        public sealed class Future : IEnumerator
        {
            /// <summary>
            /// 状态枚举
            /// </summary>
            private enum StateEnum
            {
                /** 未启动 */
                UNSTART,

                /** 加载中 */
                LOADING,

                /** 加载完成 */
                LOAD_OK,
            }

            /** 当前状态 */
            private StateEnum _currState;

            /** 敌人 Id */
            private int _enemyId;

            /** 异步操作句柄 */
            private AsyncOperationHandle<GameObject> _asyncOpHandle;

            /** 敌人对象 */
            private GameObject _enemyObj;

            /// <summary>
            /// 类参数构造器
            /// </summary>
            /// <param name="enemyId">敌人 Id</param>
            public Future(int enemyId)
            {
                _currState = StateEnum.UNSTART;
                _enemyId = enemyId;
            }

            /// <summary>
            /// 类析构函数
            /// </summary>
            ~Future()
            {
                _enemyObj = null;
            }

            // @Override
            public object Current => 1;

            // @Override
            public bool MoveNext()
            {
                // 
                // 如果是未启动状态
                ////////////////////////////////
                // 
                if (StateEnum.UNSTART == _currState)
                {
                    // 切换状态至: 加载中
                    _currState = StateEnum.LOADING;

                    // 异步方式加载文件
                    string addrezz = $"Assets/MyRes/Enemy/Enemy_{_enemyId}_/Prefab/Enemy_{_enemyId}_.prefab";
                    _asyncOpHandle = Addressables.LoadAssetAsync<GameObject>(addrezz);
                }

                // 
                // 如果是加载中状态
                ////////////////////////////////
                // 
                if (StateEnum.LOADING == _currState)
                {
                    if (null == _asyncOpHandle.Task 
                     || null != _asyncOpHandle.OperationException
                     || _asyncOpHandle.Status == AsyncOperationStatus.Failed)
                    {
                        // 如果没有启动异步操作, 
                        // 或者操作失败,
                        // 那么就不要继续等待了...
                        return false;
                    }
                    else
                    if (!_asyncOpHandle.IsDone)
                    {
                        // 如果异步操作没有完成, 
                        // 那么继续等待...
                        return true;
                    }
                    else
                    {
                        // 异步操作完成之后,
                        // 切换状态至: 加载完成
                        _currState = StateEnum.LOAD_OK;
                    }
                }

                // 
                // 如果是加载完成状态
                ////////////////////////////////
                // 
                if (StateEnum.LOAD_OK == _currState)
                {
                    GameObject goPrefab = _asyncOpHandle.Result;

                    if (null == goPrefab)
                    {
                        return false;
                    }

                    GameObject goNewEnemy = GameObject.Instantiate(goPrefab);
                    goNewEnemy.AddComponent<Enemy_X_Behav>();
                    _enemyObj = goNewEnemy;
                }

                return false;
            }

            // @Override
            public void Reset()
            {
                _currState = StateEnum.UNSTART;
                _enemyObj = null;
            }

            /// <summary>
            /// 是否为空结果
            /// </summary>
            /// <returns>true = 空结果, false = 有结果</returns>
            public bool IsEmptyResult() => null == _enemyObj;

            /// <summary>
            /// 获取敌人对象
            /// </summary>
            /// <returns>Unity 游戏对象</returns>
            public GameObject GetEnemyObj() => _enemyObj;
        }
    }
}
